import axios from 'axios';
import router from '@/router/index.js';
import store from '@/store/index.js';
import { Message } from 'element-ui'
import * as token from '@/utils/auth.js';
const PREFIX_PATH = ''

// 响应拦截器
axios.interceptors.response.use(success => {
  // 业务逻辑判断
  if(success.status && success.status == 200){
    const contentType = success.config.headers['Content-Type'];
    if(contentType){
      if(contentType.indexOf('octet-stream') > -1){
        if(success.data.msg){
          // 打印全局错误信息
          Message({
            type: 'error',
            message: success.data.msg
          })
          return;
        }
        return success.data
      }
    }
    if(success.data.code != 0){
      // 打印全局错误信息
      Message({
        type: 'error',
        message: success.data.msg
      })
      return;
    }
    // 打印成功提示信息(不需要、不一定所有请求都需要打印成功消息)
    // if(success.data.msg){
    //   store.dispatch('snackbar/openSnackbar',{
    //     msg: success.data.msg,
    //     color: 'primary'
    //   })
    // }
  }
  
  return success.data.data;
}, error => {
  if(!error.response){
    Message({
      type: 'error',
      message: '服务器连接失败!'
    })
    token.logout()
    router.replace(PREFIX_PATH+"/login")
  }else if(error.response.status == 404){
    Message({
      type: 'error',
      message: '服务器地址错误('+error.response.status+')'
    })
  }else if(error.response.status == 504){
    Message({
      type: 'error',
      message: '连接服务器错误('+error.response.status+')'
    })
  }else if(error.response.status == 500){
    Message({
      type: 'error',
      message: '连接服务器错误('+error.response.status+')'
    })
  }else{
    if(error.response.data){
      let {msg, code} = error.response.data;
      if(code){
        Message({
          type: 'error',
          message: msg+'('+code+')'
        })
        if(code == 30001){
          // 会话失效、清除localStorage信息
          token.logout()
          router.replace(PREFIX_PATH+"/login")
        }else if(code == 401){
          // 会话失效、清除localStorage信息
          token.logout()
          router.replace(PREFIX_PATH+"/login")
        }else if(code == 30003){
          // 缺少token参数、清除localStorage信息
          token.logout()
          router.replace(PREFIX_PATH+"/login")
        }
      }else{
        let enc = new TextDecoder('utf-8')
        let data = JSON.parse(enc.decode(new Uint8Array(error.response.data)))
        let {msg, code} = data;
        Message({
          type: 'error',
          message: msg+'('+code+')'
        })
      }
    }else{
      Message({
        type: 'error',
        message: '未知错误('+error.response.status+')'
      })
    }
  }
});

let base = '/api';
/**
 * 
 * @param {请求地址} url 
 * @param {请求数据} data 
 * @param {不需要携带token,默认携带} noToken 
 * @returns 
 */
export const postJson = (url, data, noToken) => {
  let config = {
    method: 'post',
    url: `${base}${url}`,
    headers: {
      "Content-Type": "application/json"
    },
    data,
  };
  if(!noToken){
    config.headers.token = token.get()
  }
  return axios(config)
}

/**
 * 
 * @param {请求地址} url 
 * @param {请求数据} data 
 * @param {不需要携带token,默认携带} noToken 
 * @returns 
 */
export const getJson = (url, noToken) => {
  let config = {
    method: 'get',
    headers: {
      // "Content-Type": "application/json",
    },
    url: `${base}${url}`
  };
  if(!noToken){
    config.headers.token = token.get()
  }
  return axios(config)
}

export const exportJson = (url, data, noToken) => {
  let config = {
    method: 'post',
    url: `${base}${url}`,
    noFormat: true,
    headers: {
      "Content-Type": "application/json; application/octet-stream"
    },
    responseType: "arraybuffer",
    data,
  };
  if(!noToken){
    config.headers.token = token.get()
  }
  return axios(config)
}